﻿//TO DO Binding !!!!!!!!!!!!!!!

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Documents;
using TvdbLib;
using TvdbLib.Data;

namespace Serietheque.Fenetres
{
    /// <summary>
    /// Logique d'interaction pour ChoixSerie.xaml
    /// </summary>
    public partial class ChoixSerie : Window
    {
        private TvdbHandler m_tvdbHandler;
        private string m_nomSerie;
        ObservableCollection<SerieChoix> m_SerieCollection;
        private SerieChoix m_selection;
        private List<TvdbSearchResult> m_series;

        internal SerieChoix Selection
        {
            get { return m_selection; }
            set { m_selection = value; }
        }

        public ChoixSerie(List<TvdbSearchResult> _series, string _nomSerie, TvdbHandler _tvdbHandler)
        {
            InitializeComponent();
            m_series = _series;
            m_SerieCollection = new ObservableCollection<SerieChoix>();
            m_nomSerie = _nomSerie;
            m_tvdbHandler = _tvdbHandler;
            if (m_nomSerie == null)
            { }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (m_series != null && m_series.Count > 0)
                foreach (TvdbSearchResult r in m_series)
                    m_SerieCollection.Add(new SerieChoix(r.Id, r.SeriesName));
            textBox1.Text = m_nomSerie;
            listBox2.ItemsSource = m_SerieCollection;
        }

        private void choisirBtn_Click(object sender, RoutedEventArgs e)
        {
            if (listBox2.SelectedItem != null)
                if (!String.IsNullOrEmpty(listBox2.SelectedItem.ToString()))
                {
                    DialogResult = true;
                    m_selection = (SerieChoix)listBox2.SelectedItem;
                    Close();
                }
        }

        private void chercherBtn_Click(object sender, RoutedEventArgs e)
        {
            m_SerieCollection.Clear();
            m_nomSerie = textBox1.Text;
            List<TvdbSearchResult> series = m_tvdbHandler.SearchSeries(m_nomSerie);
            if (series != null && series.Count > 0)
                foreach (TvdbSearchResult r in series)
                    m_SerieCollection.Add(new SerieChoix(r.Id, r.SeriesName));
        }

        private void fermerBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            m_selection = null;
        }

        private void sansTvdbBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Selection.Id = 0;
            Close();
        }
    }

    internal class SerieChoix
    {
        #region Membre Privé

        public event PropertyChangedEventHandler PropertyChanged;
        private int m_id;
        private string m_nomSerie;

        #endregion Membre Privé

        #region Membre Public

        public int Id
        {
            get { return m_id; }
            set
            {
                if (value != this.m_id)
                {
                    this.m_id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

        public string NomSerie
        {
            get { return this.m_nomSerie; }
            set
            {
                if (value != this.m_nomSerie)
                {
                    this.m_nomSerie = value;
                    NotifyPropertyChanged("NomSerie");
                }
            }
        }

        #endregion Membre Public

        public SerieChoix(int id, string nomSerie)
        {
            m_id = id;
            m_nomSerie = nomSerie;
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}